﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.Data.ProjectModule;
using EzDesk.Apps.Model.ProjectModule;
using EzDesk.Utility.ServiceStatusInfo;

namespace EzDesk.Apps.ServiceImpl.ProjectModule
{
    public partial class ProjectService
    {
        #region [ private methods ]
        private List<DailyWorks> getDailyWorks(DateTime startDate, DateTime endDate, Guid userId)
        {
            return EzDeskProjectContainer.Go(
                container =>
                    {
                        var dailyWorkses = new List<DailyWorks>();

                        var startDay = startDate.Date;
                        var endDay = endDate.Date;
                        var dailyWorks = container.DailyWorksSet.Where(
                            p => p.UserId == userId && p.Day >= startDay && p.Day <= endDay).ToList();
                        for (var theDay = startDay; theDay <= endDay; theDay = theDay.AddDays(1))
                        {
                            var day = theDay;
                            var dailyWork = dailyWorks.FirstOrDefault(p => p.Day == day);
                            if (dailyWork != null)
                            {
                                dailyWorkses.Add(
                                    new DailyWorks()
                                        {
                                            Id = dailyWork.Id,
                                            DailyWork = dailyWork.DailyWork,
                                            Day = dailyWork.Day,
                                            UserId = userId,
                                        });
                            }
                            else
                            {
                                dailyWorkses.Add(new DailyWorks { Day = day, DailyWork = "", UserId = userId });
                            }
                        }
                        return dailyWorkses;
                    });
        }
        #endregion

        public StatusInfo<DailyWorks> GetDailyWork(Guid userId, DateTime day)
        {
            return StatusInfo.Wrap(
                () => getDailyWorks(day, day, userId).FirstOrDefault());
        }

        public StatusInfo<List<DailyWorks>> GetDailyWorks(Guid userId, DateTime startDate, DateTime endDate)
        {
            return StatusInfo.Wrap(
                () => getDailyWorks(startDate, endDate, userId));
        }



        public StatusInfo<DailyWorks> UpdateDailyWorks(Guid userId, DateTime day, string content)
        {
            return StatusInfo.Wrap(
                () =>
                    {
                        using (var container = new EzDeskProjectContainer())
                        {
                            container.ContextOptions.ProxyCreationEnabled = false;
                            var dailyWork =
                                container.DailyWorksSet.FirstOrDefault(
                                    dw => dw.Day.Value == day.Date && dw.UserId == userId);
                            if (dailyWork == null)
                            {
                                dailyWork = new DailyWorks();
                                dailyWork.Id = Guid.NewGuid();
                                dailyWork.UserId = userId;
                                dailyWork.Day = day.Date;

                                container.DailyWorksSet.AddObject(dailyWork);
                            }
                            dailyWork.DailyWork = content;
                            container.SaveChanges();

                            return dailyWork;
                        }
                    }
                );
        }

    }
}
